<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="Vue2和Vue3的区别参考别人的文章和官网 1234https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_46071217&#x2F;article&#x2F;details&#x2F;108858877https:&#x2F;&#x2F;zhuanlan.zhihu.com&#x2F;p&#x2F;139590941https:&#x2F;&#x2F;v3.cn.vuejs.org&#x2F;guide&#x2F;migration&#x2F;introduction.html    从内部上响应式的改变V">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue2和Vue3的区别">
<meta property="og:url" content="http://tallgy.gitee.io/2021/10/16/Vue/vue2%E5%92%8Cvue3%E7%9A%84%E5%8C%BA%E5%88%AB/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="Vue2和Vue3的区别参考别人的文章和官网 1234https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_46071217&#x2F;article&#x2F;details&#x2F;108858877https:&#x2F;&#x2F;zhuanlan.zhihu.com&#x2F;p&#x2F;139590941https:&#x2F;&#x2F;v3.cn.vuejs.org&#x2F;guide&#x2F;migration&#x2F;introduction.html    从内部上响应式的改变V">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-10-16T12:02:19.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.354Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="Vue">
<meta property="article:tag" content="Vue3的不同">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2021/10/16/Vue/vue2%E5%92%8Cvue3%E7%9A%84%E5%8C%BA%E5%88%AB/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2021/10/16/Vue/vue2%E5%92%8Cvue3%E7%9A%84%E5%8C%BA%E5%88%AB/","path":"2021/10/16/Vue/vue2和vue3的区别/","title":"Vue2和Vue3的区别"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>Vue2和Vue3的区别 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Vue2%E5%92%8CVue3%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number">1.</span> <span class="nav-text">Vue2和Vue3的区别</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%8E%E5%86%85%E9%83%A8%E4%B8%8A"><span class="nav-number">1.1.</span> <span class="nav-text">从内部上</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%93%8D%E5%BA%94%E5%BC%8F%E7%9A%84%E6%94%B9%E5%8F%98"><span class="nav-number">1.1.1.</span> <span class="nav-text">响应式的改变</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%BB%98%E8%AE%A4%E8%BF%9B%E8%A1%8C%E6%87%92%E8%A7%82%E5%AF%9F%EF%BC%88lazy-observation%EF%BC%89%E3%80%82"><span class="nav-number">1.1.2.</span> <span class="nav-text">默认进行懒观察（lazy observation）。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9B%B4%E7%B2%BE%E5%87%86%E7%9A%84%E5%8F%98%E6%9B%B4%E9%80%9A%E7%9F%A5%E3%80%82"><span class="nav-number">1.1.3.</span> <span class="nav-text">更精准的变更通知。</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%8E%E4%BD%BF%E7%94%A8%E4%B8%8A"><span class="nav-number">1.2.</span> <span class="nav-text">从使用上</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%84%E5%90%88%E5%BC%8FAPI"><span class="nav-number">1.2.1.</span> <span class="nav-text">组合式API</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Teleport"><span class="nav-number">1.2.2.</span> <span class="nav-text">Teleport</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%89%87%E6%AE%B5"><span class="nav-number">1.2.3.</span> <span class="nav-text">片段</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8D%95%E6%96%87%E4%BB%B6%E7%BB%84%E4%BB%B6-lt-script-setup-gt"><span class="nav-number">1.2.4.</span> <span class="nav-text">单文件组件&lt;script setup&gt;</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2021/10/16/Vue/vue2%E5%92%8Cvue3%E7%9A%84%E5%8C%BA%E5%88%AB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Vue2和Vue3的区别
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-10-16 20:02:19" itemprop="dateCreated datePublished" datetime="2021-10-16T20:02:19+08:00">2021-10-16</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/Vue3/" itemprop="url" rel="index"><span itemprop="name">Vue3</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Vue2和Vue3的区别"><a href="#Vue2和Vue3的区别" class="headerlink" title="Vue2和Vue3的区别"></a>Vue2和Vue3的区别</h1><p>参考别人的文章和官网</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">https://blog.csdn.net/weixin_46071217/article/details/108858877</span><br><span class="line">https://zhuanlan.zhihu.com/p/139590941</span><br><span class="line"></span><br><span class="line">https://v3.cn.vuejs.org/guide/migration/introduction.html</span><br></pre></td></tr></table></figure>



<h2 id="从内部上"><a href="#从内部上" class="headerlink" title="从内部上"></a>从内部上</h2><h3 id="响应式的改变"><a href="#响应式的改变" class="headerlink" title="响应式的改变"></a>响应式的改变</h3><p>Vue2是使用的 <code>Object.definePropert()</code> ，而Vue3是使用的 <code>Proxy</code> 来对数据进行的代理。</p>
<p><strong>definePropert的使用</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://www.jianshu.com/p/8fe1382ba135</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">let a = &#123;&#125;;</span><br><span class="line"></span><br><span class="line">//这里这个bV是用来存储属性的值。</span><br><span class="line">let bV;</span><br><span class="line">Object.defineProperty(a, &#x27;b&#x27;, &#123;</span><br><span class="line">  set(v) &#123;</span><br><span class="line">    console.log(&#x27;set&#x27;, v);</span><br><span class="line">    bV = v;</span><br><span class="line">  &#125;,</span><br><span class="line">  get() &#123;</span><br><span class="line">    console.log(&#x27;get&#x27;, bV);</span><br><span class="line">    return bV;</span><br><span class="line">  &#125;,</span><br><span class="line">  enumerable : true,</span><br><span class="line">  configurable : true</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><strong>prosy</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">let validator = &#123;</span><br><span class="line">  set(obj, prop, value) &#123;</span><br><span class="line">    console.log(&#x27;set   &#x27;);</span><br><span class="line">    console.log(obj, prop, value);</span><br><span class="line">    obj[prop] = value;</span><br><span class="line">    return true;</span><br><span class="line">  &#125;,</span><br><span class="line">  get(obj, prop) &#123;</span><br><span class="line">    console.log(&#x27;get   &#x27;);</span><br><span class="line">    console.log(obj, prop);</span><br><span class="line">    return obj[prop];</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">let person = new Proxy(&#123;&#125;, validator);</span><br><span class="line"></span><br><span class="line">person.a = 100;</span><br><span class="line">console.log(person.a);</span><br></pre></td></tr></table></figure>

<p>proxy的优势</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">使用对象，可以是数组，函数，任何对象，而definePropert只能是一个对象的某一个值进行监听。</span><br><span class="line"></span><br><span class="line">defineProperty只能监听某个属性，不能对全对象监听</span><br><span class="line">可以省去for in、闭包等内容来提升效率（直接绑定整个对象即可）</span><br><span class="line">可以监听数组，不用再去单独的对数组做特异性操作 vue3.x可以检测到数组内部数据的变化</span><br></pre></td></tr></table></figure>



<h3 id="默认进行懒观察（lazy-observation）。"><a href="#默认进行懒观察（lazy-observation）。" class="headerlink" title="默认进行懒观察（lazy observation）。"></a>默认进行懒观察（lazy observation）。</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在 2.x 版本里，不管数据多大，都会在一开始就为其创建观察者。当数据很大时，这可能会在页面载入时造成明显的性能压力。3.x 版本，只会对「被用于渲染初始可见部分的数据」创建观察者，而且 3.x 的观察者更高效。</span><br></pre></td></tr></table></figure>



<h3 id="更精准的变更通知。"><a href="#更精准的变更通知。" class="headerlink" title="更精准的变更通知。"></a>更精准的变更通知。</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">2.x 版本中，使用 Vue.set 来给对象新增一个属性时，这个对象的所有 watcher 都会重新运行；3.x 版本中，只有依赖那个属性的 watcher 才会重新运行。</span><br></pre></td></tr></table></figure>



<h2 id="从使用上"><a href="#从使用上" class="headerlink" title="从使用上"></a>从使用上</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://v3.cn.vuejs.org/guide/migration/introduction.html</span><br></pre></td></tr></table></figure>



<h3 id="组合式API"><a href="#组合式API" class="headerlink" title="组合式API"></a>组合式API</h3><p><strong>setup</strong></p>
<p>注意</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">在 setup 中你应该避免使用 this，因为它不会找到组件实例。</span><br><span class="line">setup 的调用发生在 data property、computed property 或 methods 被解析之前，所以它们无法在 setup 中被获取。</span><br></pre></td></tr></table></figure>

<p>使用ref将值封装于对象之中。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">因为在 JavaScript 中，Number 或 String 等基本类型是通过值而非引用传递的，所以封装成对象之后变有了作用。</span><br></pre></td></tr></table></figure>

<p>在setup里面注册生命周期钩子</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">onMounted(getUserRepositories) // 在 `mounted` 时调用 `getUserRepositories`</span><br><span class="line"></span><br><span class="line">这个代码放在setup内部</span><br></pre></td></tr></table></figure>



<h3 id="Teleport"><a href="#Teleport" class="headerlink" title="Teleport"></a>Teleport</h3><p>Teleport 提供了一种干净的方法，允许我们控制在 DOM 中哪个父节点下渲染了 HTML，而不必求助于全局状态或将其拆分为两个组件。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;teleport to=&quot;body&quot;&gt;&lt;/teleport&gt;</span><br></pre></td></tr></table></figure>

<p>特点：使用之后， <code>to=&quot;body&quot;</code>，代表了在渲染时会作为body的子元素来进行渲染，不会出现一些，因为父元素的设置定位等问题而影响到了 <code>teleport </code> 的渲染。</p>
<h3 id="片段"><a href="#片段" class="headerlink" title="片段"></a>片段</h3><p>Vue2.x 不支持多根节点，而Vue3.x支持了。</p>
<p>在 3.x 中，组件可以包含多个根节点！但是，这要求开发者显式定义 attribute 应该分布在哪里。</p>
<p><strong>为什么Vue实例只能有一个根元素</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://zhuanlan.zhihu.com/p/111691226</span><br></pre></td></tr></table></figure>

<p>从源码上来说。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">在实例化Vue的时候，填写一个el选项，来指定入口</span><br><span class="line">在Vue的源码里面，他只寻找了指定选择器的第一个元素。所以后面的都会原封不动。</span><br></pre></td></tr></table></figure>

<p>对于单文件组件。</p>
<p>template里面如果有 多个div入口，不知道如何指定 谁作为入口。</p>
<h3 id="单文件组件-lt-script-setup-gt"><a href="#单文件组件-lt-script-setup-gt" class="headerlink" title="单文件组件&lt;script setup&gt;"></a>单文件组件<code>&lt;script setup&gt;</code></h3><script setup> 是在单文件组件 (SFC) 中使用组合式 API 的编译时语法糖。相比于普通的 <script> 语法，它具有更多优势：
    更少的样板内容，更简洁的代码。
    能够使用纯 Typescript 声明 props 和抛出事件。
    更好的运行时性能 (其模板会被编译成与其同一作用域的渲染函数，没有任何的中间代理)。
    更好的 IDE 类型推断性能 (减少语言服务器从代码中抽离类型的工作)。



<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">里面的代码会被编译成组件 setup() 函数的内容。这意味着与普通的 &lt;script&gt; 只在组件被首次引入的时候执行一次不同，&lt;script setup&gt; 中的代码会在每次组件实例被创建的时候执行。</span><br></pre></td></tr></table></figure>



**限制：没有 Src 导入**

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">由于模块执行语义的差异，`&lt;script setup&gt;` 中的代码依赖单文件组件的上下文。当将其移动到外部的 `.js` 或者 `.ts` 文件中的时候，对于开发者和工具来说都会感到混乱。因而 **`&lt;script setup&gt;`** 不能和 `src` attribute 一起使用。</span><br></pre></td></tr></table></figure>



### 状态驱动的动态 CSS

单文件组件的 `<style>` 标签可以通过 `v-bind` 这一 CSS 函数将 CSS 的值关联到动态的组件状态上：



<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">script </span><br><span class="line">	export default</span><br><span class="line">		data() &#123;</span><br><span class="line">			return &#123;</span><br><span class="line">				color: &#x27;red&#x27;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">.text &#123;</span><br><span class="line">  color: v-bind(color);</span><br><span class="line">&#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>



### Suspense**新增**

Suspense 是一个试验性的新特性，其 API 可能随时会发生变动。特此声明，以便社区能够为当前的实现提供反馈。

生产环境请勿使用。

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">在正确渲染组件之前进行一些异步请求是很常见的事。组件通常会在本地处理这种逻辑，绝大多数情况下这是非常完美的做法。</span><br><span class="line">该 `&lt;suspense&gt;` 组件提供了另一个方案，允许将等待过程提升到组件树中处理，而不是在单个组件中。</span><br></pre></td></tr></table></figure>

</script>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Vue/" rel="tag"># Vue</a>
              <a href="/tags/Vue3%E7%9A%84%E4%B8%8D%E5%90%8C/" rel="tag"># Vue3的不同</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/10/16/CSS/rem/" rel="prev" title="rem">
                  <i class="fa fa-chevron-left"></i> rem
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/10/17/CSS/%E6%B0%B4%E5%B9%B3%E5%9E%82%E7%9B%B4%E5%B1%85%E4%B8%AD%E6%96%B9%E6%B3%95/" rel="next" title="水平垂直居中方法">
                  水平垂直居中方法 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
